package com.mk.web;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.mk.web.mapper.BottleMapper;
import com.mk.web.mapper.PeopleMapper;
import com.mk.web.model.Bottle;
import com.mk.web.model.Flower;
import com.mk.web.model.People;
import com.mk.web.service.PeopleService;
import com.mk.web.service.impl.PeopleServiceImpl;

/**
 * Created by WangChen on 2018/11/27 22:21.
 */
public class SpringMain {

    public static void main(String [] args){
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");

//        Bottle bottle = context.getBean("bottle", Bottle.class);
//        System.out.println(bottle);
//        People people = new People();
        String[] beanDefinitionNames = context.getBeanDefinitionNames();
        for (String name : beanDefinitionNames){
            System.out.println(name);
        }
        //通过从spring container管理的SqlSessionFactory中取出sqlSessionFactory, 不用每次使用时都使用new重新生成
        SqlSessionFactory sqlSessionFactory = context.getBean("sqlSessionFactory", SqlSessionFactory.class);
        SqlSession sqlSession = sqlSessionFactory.openSession();
//        Map<String, Object> map = new HashMap<>(2);
//        map.put("name", "花2");
//        map.put("price", 6.3);
//        List<Flower> flowerList2 = sqlSession.selectList("a.b.c.selectByNameAndPrice", map);
//        BottleMapper bottleMapper = sqlSession.getMapper(BottleMapper.class);
//        List<Bottle> list = bottleMapper.selectByNameAndSize("小号", 2);

        PeopleService peopleService = context.getBean("peopleService", PeopleServiceImpl.class);
//        PeopleMapper peopleMapper = sqlSession.getMapper(PeopleMapper.class);
//        peopleService.setPeopleMapper(peopleMapper);
        List<People> people = peopleService.findByName("张三");
        System.out.println(people);
//        for (Bottle bottle : list){
//            System.out.println(bottle);
//        }

        sqlSession.close();
    }
}
